﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Analysis.Code;
using Analysis.DAL.Repositories;

namespace Analysis.Controls
{
    public partial class CustomColumnDialog : System.Web.UI.UserControl
    {
        public bool AllowAdd { get; set; }
        public bool AllowVisible { get; set; }
        
        public GridView MainGrid
        {
            get
            {
                return (Page as BaseGridPage).GetGrid();
            }
        }
        public string TableName 
        {
            get
            {
                return (Page as BaseGridPage).PageName;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void upColumnDetails_Load(object sender, EventArgs e)
        {
        }

        public void LoadData(List<ColumnInfo> dataSource)
        {
            gridCustomColumn.DataSource = dataSource;
            gridCustomColumn.DataBind();
            upColumnDetails.Update();

            btnAddCustomColumn.Visible = AllowAdd;
            gridCustomColumn.Columns[0].Visible = AllowVisible;
            gridCustomColumn.Columns[3].Visible = AllowAdd;
        }

        protected void gridCustomColumn_RowDataBound(Object Sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ColumnInfo item = (ColumnInfo)e.Row.DataItem;

                ImageButton btn = (ImageButton)e.Row.FindControl("btnDelete");
                btn.Visible = item.IsCustom;

                TextBox columnName = (TextBox)e.Row.FindControl("tbCustomColumnName");
                columnName.ReadOnly = !item.IsCustom;

            }
        }

        protected void gridCustomColumn_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string columnHeader = e.CommandArgument.ToString();
            string columnName = "Custom " + e.CommandArgument.ToString();

            string result = SaveCustomColumn();
            if (!string.IsNullOrEmpty(result))
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", string.Format("alert('{0}');", result), true);
                return;
            }

            switch (e.CommandName)
            {
                case "MakeDelete":
                    APRepository.CustomColumnDelete(TableName, columnName);
                    (Page as BaseGridPage).RemoveCustomColumn(columnHeader);

                    break;
            }
        }

        protected void btnSaveCustomColumn_Click(object sender, EventArgs e)
        {
            string result = SaveCustomColumn();
            if (!string.IsNullOrEmpty(result))
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", string.Format("alert('{0}');", result), true);
                return;
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "$('#columnDialog').dialog('close');", true);
            }
        }

        protected void btnAddCustomColumn_Click(object sender, EventArgs e)
        {
            string result = SaveCustomColumn();
            if (!string.IsNullOrEmpty(result))
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", string.Format("alert('{0}');", result), true);
                return;
            }
                        
            BaseDataRepository.CustomColumnAdd(TableName);
            (Page as BaseGridPage).AddCustomColumn();
        }

        protected string SaveCustomColumn()
        {
            string result = "";

            string CustomColumnNameOld = "";
            string CustomColumnName = "";

            if (AllowAdd)
            {
                foreach (GridViewRow item in gridCustomColumn.Rows)
                {
                    if (item.RowType != DataControlRowType.DataRow)
                        continue;

                    string oldName = ((HiddenField)item.FindControl("hidCustomColumnNameOld")).Value;
                    string newName = ((TextBox)item.FindControl("tbCustomColumnName")).Text;

                    CustomColumnNameOld += "Custom " + oldName + ";";
                    CustomColumnName += "Custom " + newName + ";";
                }
                result = BaseDataRepository.CustomColumnEdit(TableName, CustomColumnNameOld, CustomColumnName);
            }

            if (string.IsNullOrEmpty(result))
            {
                foreach (GridViewRow item in gridCustomColumn.Rows)
                {
                    if (item.RowType != DataControlRowType.DataRow)
                        continue;

                    string oldName = ((HiddenField)item.FindControl("hidCustomColumnNameOld")).Value;
                    string newName = ((TextBox)item.FindControl("tbCustomColumnName")).Text;

                    // запомним ширину
                    string colWidth = ((TextBox)item.FindControl("tbColumnWidth")).Text;
                    APRepository.SetUserParam((Page as BaseGridPage).CurrentUserId, string.Format("{0}_grid_{1}_ColumnWidth", TableName, newName), colWidth);
                    
                    // запомним видимость
                    if (AllowVisible)
                    {
                        bool isVisible = ((CheckBox)item.FindControl("chbIsVisible")).Checked;
                        APRepository.SetUserParam((Page as BaseGridPage).CurrentUserId, string.Format("{0}_grid_{1}_ColumnVisible", TableName, newName), isVisible?"1":"0");
                    }

                    if ((oldName != newName) && (AllowAdd))
                    {
                        (Page as BaseGridPage).EditCustomColumn(oldName, newName);
                    }
                }

                (Page as BaseGridPage).LoadDataFromDb();
                (Page as BaseGridPage).LoadData();
            }
            
            return result;
        }
    }
}